﻿var UserInfoMgr = {
    log: "sv.user.log",
    userInfo: {
        id: "sv.user.id",
        passWord: "sv.user.pwd",
        openId: "sv.user.openid",
        unpickedNum: "sv.user.unpicknum",
        score: "sv.user.score",
        scale2Cash: "sv.user.scale2cash",
    },
    historyPoi: {
        list: "sv.user.his.poi.list",
    },
    recentPoint: {
        list: "sv.user.recent.poi.list",
    },
    loginStatus: 1,
    logoutStatus: 0,

    isLogIn: function () {
        var isLog = false;
        var logVal = getStorageVal(UserInfoMgr.log);
        if (logVal != null && (Number(logVal) == UserInfoMgr.loginStatus)) {
            isLog = true;
        }
        return isLog;
    },
    logIn: function (userId, openId, unpickedNum) {
        setStorage(UserInfoMgr.log, UserInfoMgr.loginStatus);
        setStorage(UserInfoMgr.userInfo.id, userId);
        setStorage(UserInfoMgr.userInfo.openId, openId);
        setStorage(UserInfoMgr.userInfo.unpickedNum, unpickedNum);
    },
    //微信openId登录
    weixinLogin: function (loginCallback, logoutCallback) {
        var isLog = UserInfoMgr.isLogIn();
        if (isLog) {
            if (loginCallback) {
                loginCallback();
            }
        } else {
            var openId = UserInfoMgr.getOpenId();
            if (null == openId) {
                if (logoutCallback) {
                    logoutCallback();
                }
            }
            var command = "wx_login";
            var weixinLoginRequestJson = {
                "openid": openId
            }
            doAjax(weixinLoginRequestJson, command, function (result) {
                if (result.result == 0) {
                    UserInfoMgr.logIn(result.wx_user_info.userid, result.wx_user_info.openid, result.wx_user_info.unpicked_order_num);
                    if (loginCallback) {
                        loginCallback();
                    }
                } else {
                    UserInfoMgr.resetUserBaseInfo();
                    if (logoutCallback) {
                        logoutCallback();
                    }
                }
            });
        }
    },
    //绑定超人账号
    weixinUserBind: function (userId, identifying_code, successCallback, errorCallback) {
        var openId = UserInfoMgr.getOpenId();
        if (null == openId) {
            if (errorCallback) {
                errorCallback();
            }
        }
        var command = "wx_2_superstore";
        var weixinUserBindRequestJson = {
            "openid": openId,
            "userid": userId,
            "identifying_code": identifying_code
        }
        doAjax(weixinUserBindRequestJson, command, function (result) {
            if (result.result == 0) {
                UserInfoMgr.logIn(result.wx_user_info.userid, result.wx_user_info.openid, result.wx_user_info.unpicked_order_num);
                if (successCallback) {
                    successCallback();
                }
            } else if (result.result == 8) {
                if (errorCallback) {
                    errorCallback();
                }
            } else {
                UserInfoMgr.resetUserBaseInfo();
            }
        });
    },
    resetUserBaseInfo: function () {
        setStorage(UserInfoMgr.log, UserInfoMgr.logoutStatus);
        removeStorage(UserInfoMgr.userInfo.id);
        removeStorage(UserInfoMgr.userInfo.passWord);
        removeStorage(UserInfoMgr.userInfo.unpickedNum);
        removeStorage(UserInfoMgr.userInfo.score);
        removeStorage(UserInfoMgr.userInfo.scale2Cash);
    },
    setOpenId: function (openId, callback) {
        setStorage(UserInfoMgr.userInfo.openId, openId);
        if (callback) {
            callback(UserInfoMgr.getOpenId());
        }
    },
    getOpenId: function () {
        return getStorageVal(UserInfoMgr.userInfo.openId);
    },
    getUserID: function () {
        var userId = null;
        if (UserInfoMgr.isLogIn()) {
            userId = getStorageVal(UserInfoMgr.userInfo.id);
        }
        return userId;
    },
    getPassWord: function () {
        var password = null;
        if (UserInfoMgr.isLogIn()) {
            password = getStorageVal(UserInfoMgr.userInfo.passWord);
        }
        return password;
    },
    getUnpickNum: function () {
        var unpickNum = 0;
        if (UserInfoMgr.isLogIn()) {
            unpickNum = Number(getStorageVal(UserInfoMgr.userInfo.unpickedNum));
        }
        return unpickNum;
    },
    addunpickNum: function (num) {
        if (UserInfoMgr.isLogIn()) {
            var unpickNum = Number(getStorageVal(UserInfoMgr.userInfo.unpickedNum));
            unpickNum += num;
            setStorage(UserInfoMgr.userInfo.unpickedNum, unpickNum);
        }
    },
    getScore: function () {
        var score = 0;
        if (UserInfoMgr.isLogIn()) {
            score = Number(getStorageVal(UserInfoMgr.userInfo.score));
        }
        return score;
    },
    getscale2Cash: function () {
        var scale2Cash = 0;
        if (UserInfoMgr.isLogIn()) {
            scale2Cash = Number(getStorageVal(UserInfoMgr.userInfo.scale2Cash));
        }
        return scale2Cash;
    },
    setHistoryPoiList: function (hostoryPoiList) {
        if (UserInfoMgr.isLogIn()) {
            setStorage(UserInfoMgr.historyPoi.list, hostoryPoiList);
        }
    },
    getHistoryPoiList: function () {
        var historyPoiList = null;
        if (UserInfoMgr.isLogIn()) {
            historyPoiList = getStorageVal(UserInfoMgr.historyPoi.list);
        }
        return historyPoiList;
    },
    resetHistoryPoiList: function () {
        removeStorage(UserInfoMgr.historyPoi.list);
    },
    getHistoryPoiListData: function (showHistoryPoint, hideHistoryPoint) {
        var isLog = UserInfoMgr.isLogIn();
        if (isLog) {
            UserInfoMgr.showHistoryPoiListData(showHistoryPoint, hideHistoryPoint);
        } else {
            hideHistoryPoint();
        }
    },
    showHistoryPoiListData: function (showHistoryPoint, hideHistoryPoint) {
        var historyPointList = UserInfoMgr.getHistoryPoiList();
        if (null == historyPointList) {
            UserInfoMgr.getHistoryPointFromService(showHistoryPoint, hideHistoryPoint);
        } else {
            var myObj = eval(historyPointList);
            if (myObj.length > 0) {
                showHistoryPoint(myObj);
            } else {
                hideHistoryPoint();
            }
        }
    },
    getHistoryPointFromService: function (showHistoryPoint, hideHistoryPoint) {
        var sessionId = UserInfoMgr.getSessionId();
        var historyPointRequestJson = {
            "session_id": sessionId
        }
        var command = "history_poi";
        doAjax(historyPointRequestJson, command, function (result) {
            if (result.result == 0) {
                UserInfoMgr.setHistoryPoiList(result.address_info);
                showHistoryPoint(result.address_info);
            } else {
                hideHistoryPoint();
            }
        });
    },
    setRecentPointList: function (recentPointList) {
        if (UserInfoMgr.isLogIn()) {
            setStorage(UserInfoMgr.recentPoint.list, recentPointList);
        }
    },
    getRecentPointList: function () {
        var recentPointList = null;
        if (UserInfoMgr.isLogIn()) {
            recentPointList = getStorageVal(UserInfoMgr.recentPoint.list);
        }
        return recentPointList;
    },
    resetRecentPointList: function () {
        removeStorage(UserInfoMgr.recentPoint.list);
    },
    getRecentPointListData: function (showRecentPoint, hideRecentPoint) {
        var isLog = UserInfoMgr.isLogIn();
        if (isLog) {
            UserInfoMgr.showRecentPointListData(showRecentPoint, hideRecentPoint);
        } else {
            hideRecentPoint();
        }
    },
    showRecentPointListData: function (showRecentPoint, hideRecentPoint) {
        var recentPointList = UserInfoMgr.getRecentPointList();
        if (null == recentPointList) {
            UserInfoMgr.getRecentPointFromService(showRecentPoint, hideRecentPoint);
        } else {
            var myObj = eval(recentPointList);
            if (myObj.length > 0) {
                showRecentPoint(myObj);
            } else {
                hideRecentPoint();
            }
        }
    },
    getRecentPointFromService: function (showRecentPoint, hideRecentPoint) {
        var curTerminalInfo = TerminalMgr.getTerminalInfo();
        var sessionId = UserInfoMgr.getSessionId();
        var recentPointRequestJson = {
            "type": curTerminalInfo.type,
            "session_id": sessionId
        };
        var command = "recently_point";
        doAjax(recentPointRequestJson, command, function (result) {
            if (result.result == 0) {
                UserInfoMgr.setRecentPointList(result.point_info);
                showRecentPoint(result.point_info);
            } else {
                hideRecentPoint();
            }
        });
    },
    getSessionId: function () {
        var sessionId = null;
        if (UserInfoMgr.isLogIn()) {
            var openId = getStorageVal(UserInfoMgr.userInfo.openId);
            sessionId = {
                "openid": openId
            };
        }
        return sessionId;
    }
};